package com.tech.connect.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ksy.common.image.ImageLoader;
import com.ksy.common.utils.NumUtils;
import com.tech.connect.R;
import com.tech.connect.activity.AuthInfoActivity;
import com.tech.connect.activity.EditUserInfoActivity;
import com.tech.connect.activity.SettingActivity;
import com.tech.connect.activity.UserInfoActivity;
import com.tech.connect.activity.VipInfoActivity;
import com.tech.connect.activity.WalletActivity;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.HttpUtils;
import com.tech.connect.api.UserInfo;

public class MainTabMineFragment extends BaseFragment {

    private TextView tvRenzheng;
    private ImageView ivCode;
    private ImageView ivHead;
    private TextView tvName;
    private TextView tvSexAndAge;
    private TextView tvInfo;
    private ImageView ivEdit;
    private ImageView ivVip;
    private TextView tvVipDate;
    private TextView tvVipCharge;
    private ImageView ivCover;

    @Override
    public View onCreateView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_main_user, null);
        return view;
    }

    @Override
    protected boolean isShowHeadBar() {
        return false;
    }

    @Override
    protected boolean isFullStateBar() {
        return true;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData();
        loadData();
    }

    private void initView(View view) {
        ivCover = view.findViewById(R.id.ivCover);
        ivCode = view.findViewById(R.id.ivCode);
        ivHead = view.findViewById(R.id.ivHead);
        ivHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadData();
            }
        });
        tvName = view.findViewById(R.id.tvName);
        tvSexAndAge = view.findViewById(R.id.tvSexAndAge);
        ivEdit = view.findViewById(R.id.ivEdit);
        tvInfo = view.findViewById(R.id.tvInfo);
        ivVip = view.findViewById(R.id.ivVip);
        tvVipDate = view.findViewById(R.id.tvVipDate);
        tvVipCharge = view.findViewById(R.id.tvVipCharge);
        ivEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(EditUserInfoActivity.class, 996);
            }
        });

        View rlHuiyuanzhongxin = view.findViewById(R.id.rlHuiyuanzhongxin);
        View rlGerenzhongxin = view.findViewById(R.id.rlGerenzhongxin);
        View rlRenzhengzhongxin = view.findViewById(R.id.rlRenzhengzhongxin);
        tvRenzheng = view.findViewById(R.id.tvRenzheng);
        View rlQianbao = view.findViewById(R.id.rlQianbao);
        View rlShoucang = view.findViewById(R.id.rlShoucang);
        View rlXiangce = view.findViewById(R.id.rlXiangce);
        View rlKefu = view.findViewById(R.id.rlKefu);
        View rlShezhi = view.findViewById(R.id.rlShezhi);
        rlHuiyuanzhongxin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(VipInfoActivity.class);
            }
        });
        rlGerenzhongxin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(UserInfoActivity.class);
            }
        });
        rlRenzhengzhongxin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(AuthInfoActivity.class);
            }
        });
        rlQianbao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(WalletActivity.class);
            }
        });
        rlShoucang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(WalletActivity.class);
            }
        });
        rlXiangce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(WalletActivity.class);
            }
        });
        rlKefu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(WalletActivity.class);
            }
        });
        rlShezhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(SettingActivity.class);
            }
        });
    }

    private void loadData() {
        HttpUtils.getUserInfo(new BaseEntityOb<UserInfo>() {
            @Override
            public void onDataDeal(boolean isSuccess, UserInfo data, String msg) {
                UserInfo.getUserInfo().setDataInfoByLogIn(data);
                initData();
            }
        });
    }

    private void initData() {
        UserInfo userInfo = UserInfo.getUserInfo();
        if (userInfo.isIdentityAuth == 1 || userInfo.isMobileAuth == 1 || userInfo.isSkillAuth == 1) {
            tvRenzheng.setText("已认证");
        } else {
            tvRenzheng.setText("未认证");
        }

        ImageLoader.getInstance().loadHead(activity, userInfo.headImage, ivHead);
        ImageLoader.getInstance().load(activity, userInfo.backgroundImage, ivCover);
        tvName.setText(userInfo.nickName);
        String sex = "";
        if (userInfo.gender == 0) {
            sex = "女";
        } else if (userInfo.gender == 1) {
            sex = "男";
        } else {
            sex = "保密";
        }
        tvSexAndAge.setText(sex + " " + String.valueOf(userInfo.age));
        String provinceName = userInfo.provinceName;
        String cityName = userInfo.cityName;

        StringBuilder info = new StringBuilder();
        if (isNotEmpty(provinceName)) {
            info.append(provinceName).append(" ");
        }
        if (isNotEmpty(cityName)) {
            info.append(cityName).append(" ");
        }
        info.append(NumUtils.getScale2String(userInfo.height)).append("cm");
        info.append(" ");
        info.append(NumUtils.getScale2String(userInfo.weight)).append("kg");
        tvInfo.setText(info.toString());
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (Activity.RESULT_OK == resultCode) {
            if (requestCode == 996) {
                initData();
            }
        }
    }
}
